package com.campsys.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campsys.entity.Activity;
import com.campsys.entity.CampClass;
import com.campsys.mapper.ActivityMapper;
import com.campsys.mapper.CampClassMapper;
import com.campsys.service.ActivityService;
import com.campsys.service.dto.ActivityDTO;
import com.campsys.service.dto.ActivityQueryDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
@Slf4j
public class ActivityServiceImpl implements ActivityService {
    
    @Autowired
    private ActivityMapper activityMapper;
    
    @Autowired
    private CampClassMapper classMapper;
    
    @Override
    public IPage<Activity> getActivityList(Page<Activity> page, ActivityQueryDTO queryDTO) {
        try {
            return activityMapper.selectActivityList(page, queryDTO);
        } catch (Exception e) {
            log.error("获取活动列表失败", e);
            throw new RuntimeException("获取活动列表失败", e);
        }
    }
    
    @Override
    public void addActivity(ActivityDTO activityDTO) {
        if (activityDTO == null) {
            throw new IllegalArgumentException("活动信息不能为空");
        }
        Activity activity = new Activity();
        BeanUtils.copyProperties(activityDTO, activity);
        activity.setStatus(1);  // 默认开放
        activity.setCreateTime(LocalDateTime.now());
        activity.setCreateBy("admin");  // TODO: 从当前登录用户获取
        activityMapper.insert(activity);
    }
    
    @Override
    public void updateActivity(ActivityDTO activityDTO) {
        if (activityDTO == null) {
            throw new IllegalArgumentException("活动信息不能为空");
        }
        Activity activity = new Activity();
        BeanUtils.copyProperties(activityDTO, activity);
        activity.setUpdateTime(LocalDateTime.now());
        activity.setUpdateBy("admin");  // TODO: 从当前登录用户获取
        activityMapper.updateById(activity);
    }
    
    @Override
    @Transactional
    public void deleteActivity(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("活动ID不能为空");
        }
        
        try {
            // 先删除关联的教学班
            LambdaQueryWrapper<CampClass> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(CampClass::getActivityId, id);
            classMapper.delete(wrapper);
            
            // 再删除活动
            activityMapper.deleteById(id);
            
        } catch (Exception e) {
            log.error("删除活动失败", e);
            throw new RuntimeException("删除活动失败：" + e.getMessage());
        }
    }
    
    @Override
    public void updateStatus(Long id, Integer status) {
        if (id == null) {
            throw new IllegalArgumentException("活动ID不能为空");
        }
        if (status == null || (status != 0 && status != 1)) {
            throw new IllegalArgumentException("无效的状态值");
        }
        Activity activity = new Activity();
        activity.setId(id);
        activity.setStatus(status);
        activity.setUpdateTime(LocalDateTime.now());
        activity.setUpdateBy("admin");  // TODO: 从当前登录用户获取
        activityMapper.updateById(activity);
    }
} 